package edge.display.backend.Utils;

import com.alibaba.fastjson.JSONObject;
import edge.display.backend.Entity.Task;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.*;

/**
 * 任务工具类
 *
 * @author Amane Hayaashi
 * @date 2020/11/6
 * @since 1.0
 */

public class TaskUtils {

    public static void updateTimestamps(Task task, JSONObject jsonObject, String... keys) {
        if (Objects.isNull(task)) {
            return;
        }
        List<String> timestamps;
        if (Objects.isNull(task.getTimestamps())) {
            timestamps = new ArrayList<>();
        } else {
            timestamps = task.getTimestamps();
        }
        for (String k : keys) {
            String ts = jsonObject.getString(k);
            if (Objects.isNull(ts) || "".equals(ts)) {
                ts = String.valueOf(new Date().getTime());
            }
            ts = ts.substring(0, 13);
            timestamps.add(ts);
        }
        task.setTimestamps(timestamps);
    }

    public static void IP2Index(JSONObject jsonObject, String key, Map<String, Integer> map) {
        String ip = jsonObject.getString(key);
        int index = map.getOrDefault(ip, -1);
        jsonObject.put(key, index);
    }

    public static void cutImage(String path, String imageFormatName, float h2w) {
        FileInputStream fileInputStream = null;
        ImageInputStream iis = null;
        try {
            fileInputStream = new FileInputStream(path);
            Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(imageFormatName);
            ImageReader reader = it.next();
            iis = ImageIO.createImageInputStream(fileInputStream);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            int index = 0;
            int w0 = reader.getWidth(index), h0 = reader.getHeight(index);
            int h, w, x, y;
            // judge by the height-width ratio
            if ((float) h0 / (float) w0 < h2w) {
                h = h0;
                w = (int) (h0 / h2w);
                x = (w0 - w) / 2;
                y = 0;
            } else {
                w = w0;
                h = (int) (w * h2w);
                x = 0;
                y = (h0 - h) / 2;
            }
            Rectangle rect = new Rectangle(x, y, w, h);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(index, param);
            ImageIO.write(bi, imageFormatName, new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (iis != null) {
                    iis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
